package com.yic.module.device.service.device;

import cn.hutool.core.util.ObjectUtil;
import com.yic.framework.common.exception.ErrorCode;
import com.yic.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.yic.framework.common.util.object.CompareObjectUtils;
import com.yic.module.device.service.resume.ResumeService;
import com.yic.module.system.enums.dict.DeviceResumeTypeEnum;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.yic.module.device.controller.admin.device.vo.*;
import com.yic.module.device.dal.dataobject.device.DeviceDO;
import com.yic.framework.common.pojo.PageResult;

import com.yic.module.device.convert.device.DeviceConvert;
import com.yic.module.device.dal.mysql.device.DeviceMapper;

import static com.yic.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yic.module.base.enums.ErrorCodeConstants.SUPPLIER_CODE_EXISTS;
import static com.yic.module.device.enums.ErrorCodeConstants.*;

/**
 * 设备基本信息 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class DeviceServiceImpl implements DeviceService {

    @Resource
    private DeviceMapper deviceMapper;
    @Resource
    private ResumeService resumeService;
    @Resource
    private TransactionTemplate transactionTemplate;

    @Override
    @Transactional
    public Long create(DeviceCreateReqVO createReqVO) {
        validateCreateOrUpdate(null, createReqVO.getCode());
        // 插入
        DeviceDO device = DeviceConvert.INSTANCE.convert(createReqVO);
        deviceMapper.insert(device);
        resumeService.createResume(device.getId(), DeviceResumeTypeEnum.CREATE.getDeviceResumeType(), "设备登记", device.getId(), Long.valueOf(device.getCreator()), device.getCreateTime());
        // 返回
        return device.getId();
    }

    @Override
    public void update(DeviceUpdateReqVO updateReqVO) {
        // 校验存在
        validateCreateOrUpdate(updateReqVO.getId(), updateReqVO.getCode());
        DeviceDO updateObj = DeviceConvert.INSTANCE.convert(updateReqVO);
        deviceMapper.updateById(updateObj);
    }

    private String compareChange(DeviceDO oldObj, DeviceDO newObj) throws Exception {
        List<Map<String, Object>> list = CompareObjectUtils.compareTwoClass(oldObj, newObj);
        String content = "";
        for (Map<String, Object> map : list) {
            if (map.get("old") == null) {
                map.put("old", "无");
            }
            content += map.get("name") + ":" + map.get("old") + " 变更为 " + map.get("new") + "。";
        }
        return content;
    }

    @Override
    public void delete(Long id) {
        // 校验存在
        validateExists(id);
        // 删除
        deviceMapper.deleteById(id);
    }

    private void validateCreateOrUpdate(Long id, String code){
        validateCodeExist(id, code);
    }

    private void validateCodeExist(Long id, String code){
        DeviceDO device = deviceMapper.selectByCode(code);
        if (ObjectUtil.isEmpty(device)){
            return;
        }
        if (id == null){
            throw exception(DEVICE_CODE_EXISTS);
        }
        if (!id.equals(device.getId())){
            throw exception(DEVICE_CODE_EXISTS);
        }
    }

    private void validateExists(Long id) {
        if (deviceMapper.selectById(id) == null) {
            throw exception(DEVICE_NOT_EXISTS);
        }
    }

    @Override
    public DeviceDO get(Long id) {
        return deviceMapper.selectById(id);
    }

    @Override
    public List<DeviceDO> getList(Collection<Long> ids) {
        return deviceMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<DeviceDO> getPage(DevicePageReqVO pageReqVO) {
        return deviceMapper.selectPage(pageReqVO);
    }

    @Override
    public List<DeviceDO> getList(DeviceExportReqVO exportReqVO) {
        return deviceMapper.selectList(exportReqVO);
    }

    @Override
    public List<DeviceDO> getDeviceList(Collection<Long> ids, Collection<Integer> statuses) {
        return deviceMapper.selectList(ids, statuses);
    }

    @Override
    public void updateDeviceStatus(Long deviceId, Integer status, Integer useStatus) {
        DeviceDO deviceDO = new DeviceDO();
        deviceDO.setId(deviceId);
        if (ObjectUtil.isNotNull(status)) {
            deviceDO.setStatus(status);
        }
        if (ObjectUtil.isNotNull(useStatus)) {
            deviceDO.setUseStatus(useStatus);
        }
        deviceMapper.updateById(deviceDO);
    }

    @Override
    public void updateReceiveInfo(Long deviceId, Long user, String loacation, Long deptId, Long factoryId, Long centerId, Integer deviceUseStatus) {
        DeviceDO deviceDO = new DeviceDO();
        deviceDO.setId(deviceId);
        deviceDO.setUserId(user);
        deviceDO.setLoacation(loacation);
        deviceDO.setDeptId(deptId);
        deviceDO.setFactoryId(factoryId);
        deviceDO.setCenterId(centerId);
        deviceDO.setUseStatus(deviceUseStatus);
        deviceMapper.updatePartById(deviceDO);
    }

}
